package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.PageResult;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.common.utils.CurrentUserContextHolders;
import com.qk.domain.business.BusinessSelectByPageDO;
import com.qk.dto.business.BusinessFollowBusinessDTO;
import com.qk.dto.business.BusinessSelectByPageDTO;
import com.qk.dto.business.BusinessSelectPoolByPageDTO;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.User;
import com.qk.management.mapper.BusinessMapper;
import com.qk.management.mapper.BusinessTrackRecordMapper;
import com.qk.management.mapper.UserMapper;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<BusinessSelectByPageDO> selectByPage(BusinessSelectByPageDTO businessSelectByPageDTO) {
        PageHelper.startPage(businessSelectByPageDTO.getPage(),businessSelectByPageDTO.getPageSize());
        PageInfo<BusinessSelectByPageDO> pageInfo = new PageInfo<>(this.baseMapper.selectByPage(businessSelectByPageDTO));
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void addBusiness(Business business) {
//        参数校验,name和phone不能为空,其他可以为空
        boolean hasNull = BeanUtil.hasNullField(business, "name", "phone");
        if (!hasNull) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }

        business.setStatus(BusinessStatusConstants.WAIT_ALLOCATION);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
//        设置一个默认渠道
        if(business.getChannel() == null)
            business.setChannel(1);
        this.baseMapper.insert(business);
    }

    @Override
    public void assignBusiness(Integer businessId, Integer userId) {
        Business business = this.baseMapper.selectById(businessId);
        business.setUserId(userId);
        business.setUpdateTime(LocalDateTime.now());
        business.setStatus(BusinessStatusConstants.WAIT_FOLLOW_UP);
        this.baseMapper.updateById(business);
    }

    @Override
    public void backToPool(Integer id) {
        Business business = this.baseMapper.selectById(id);
        business.setStatus(BusinessStatusConstants.RECOVERY);
        business.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(business);
    }

    @Override
    public void businessToCustomer(Integer id) {
        Business business = this.baseMapper.selectById(id);
        business.setStatus(BusinessStatusConstants.TRANSFORM_CUSTOMER);
        business.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(business);
    }

    @Override
    public BusinessVO getBusinessById(Integer id) {
        Business business = this.baseMapper.selectById(id);
        User user = userMapper.selectById(business.getUserId());
        List<BusinessTrackRecord> businessTrackRecords = businessTrackRecordMapper
                .selectList(Wrappers
                        .<BusinessTrackRecord>lambdaQuery()
                        .in(BusinessTrackRecord::getBusinessId, id));
        BusinessVO businessVO = BeanUtil.copyProperties(business, BusinessVO.class);
        List<BusinessVO.BusinessTrackRecordsVO> list = businessTrackRecords.stream().map(item -> {
            BusinessVO.BusinessTrackRecordsVO businessTrackRecordsVO = BeanUtil.copyProperties(item, BusinessVO.BusinessTrackRecordsVO.class);
            businessTrackRecordsVO.setAssignName(user.getName());
            return businessTrackRecordsVO;
        }).toList();


        businessVO.setTrackRecords(list);
        return businessVO;
    }

    @Override
//    事务管理
    @Transactional
    public void followBusiness(BusinessFollowBusinessDTO dto) {

        Business business = BeanUtil.copyProperties(dto, Business.class);
        business.setStatus(BusinessStatusConstants.FOLLOW_UP);
        business.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(business);


        BusinessTrackRecord businessTrackRecord = BeanUtil.copyProperties(dto, BusinessTrackRecord.class);
        businessTrackRecord.setCreateTime(LocalDateTime.now());
        businessTrackRecord.setId( null);
        businessTrackRecord.setBusinessId(dto.getId());
        businessTrackRecord.setUserId(CurrentUserContextHolders.get());
        this.businessTrackRecordMapper.insert(businessTrackRecord);
    }

    @Override
    public PageResult<Business> selectPoolByPage(BusinessSelectPoolByPageDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        PageInfo<Business> pageInfo = new PageInfo<>(this.baseMapper.selectPoolByPage(dto));
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }
}
